#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>


int main()
{
  //打开文件            写的方式   是否创建  存在先清空
  //int fd = open("log.txt",O_RDWR);
  
  //dup2(fd,1);
  
  int fd = open("log.txt",O_RDONLY);
  dup2(fd,0);
  const char *buffer[1024];
  size_t s = read(0,buffer,sizeof(buffer));
  if(s > 0)
  {
    buffer[s]=0;
    printf("stdin redir:\n%s\n",buffer);
  }



  ////原本应该写在1中现在重定向到fd中
  //dup2(fd,1);
  //write(1,"hello 1\n",8);
  //printf("hello fd:%d\n",fd);
  //fprintf(stdout,"hello fd:%d\n",fd);
  //fputs("hello world\n",stdout);

  //const char *message = "hello fwrite\n";
  //fwrite(message,1,strlen(message),stdout);



  //close();?????






  //close(1);
  //int fd1 = open("log1.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  //int fd2 = open("log2.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  //int fd3 = open("log3.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  //int fd4 = open("log4.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  //printf("fd1: %d\n",fd1);
  //printf("fd2: %d\n",fd2);
  //printf("fd3: %d\n",fd3);
  //printf("fd4: %d\n",fd4);
  //fflush(stdout);

  //close(fd1);
  //close(fd2);
  //close(fd3);
  //close(fd4);




















  //printf("stdin: %d\n",stdin->_fileno);
  //printf("stdout: %d\n",stdout->_fileno);
  //printf("stderr: %d\n",stderr->_fileno);

  ////打开文件
  //FILE* fp = fopen("log.txt","w");
  //printf("fp: %d\n",fp->_fileno);
  ////umask(0);
  //如果这个文件没有需要重新创建就要用到O_CREATE，mode是用来初始化权限的
  //               文件名      写    是否创建 是否先清空文件
  //int fdl = open("log.txt",O_WRONLY | O_CREAT | O_APPEND,0666);
  //if(fdl < 0)
  //{
  //  perror("open");
  //  return 1;
  //}
  //printf("fdl: %d\n",fdl);
  //const char *message = "hello file\n";
  ////将字符写到文件当中
  //write(fdl,message,strlen(message));
  ////打开文件
  //close(fdl);
  //return 0;
}






















//#include<stdio.h>
////one shift left one position
//#define ONE (1<<0)
//#define TWO (1<<1)
//#define THREE (1<<2)
//#define FOUR (1<<3)
//#define FIVE (1<<4)
////code 1
//void PrintTest(int flags)
//{
//  //if this is true,indicate flags is ONE 
//  if(flags & ONE)
//  {
//    printf("one\n");
//  }
//  if(flags & TWO)
//  {
//    printf("two\n");
//  }
//  if(flags & THREE)
//  {
//    printf("three\n");
//  }
//  if(flags & FOUR)
//  {
//    printf("four\n");
//  }
//  if(flags & FIVE)
//  {
//    printf("five\n");
//  }
//}
//
//int main()
//{
//  printf("===========================================\n");
//  PrintTest(ONE);
//  PrintTest(ONE | TWO | THREE);
//  printf("===========================================\n");
//  return 0;
//}
